package com.zmm.myyimixian.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zmm.myyimixian.R;
import com.zmm.myyimixian.db.Address;
import com.zmm.myyimixian.util.UIManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/2 0002.
 */
public class AddressAdapter extends BaseAdapter {

    private List<Address> mList;


    public void setData(List<Address> list) {
        this.mList = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.address_list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //绑定数据
        final Address address = mList.get(position);
        holder.addressListName.setText(address.getUserName());
        holder.addressListSex.setText(address.getUserSex());
        holder.addressListPhone.setText(address.getUserPhone());
        holder.addressListTag.setText(address.getUserTag());
        holder.addressListDetailed.setText(address.getUserDetailedAddress());
        holder.addressListBuilding.setText(address.getUserBuilding());
        holder.addressListEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManager.startModificationAddressActivity(parent.getContext(),address);
            }
        });
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.address_list_name)
        TextView addressListName;
        @BindView(R.id.address_list_sex)
        TextView addressListSex;
        @BindView(R.id.address_list_phone)
        TextView addressListPhone;
        @BindView(R.id.address_list_detailed)
        TextView addressListDetailed;
        @BindView(R.id.address_list_building)
        TextView addressListBuilding;
        @BindView(R.id.address_list_tag)
        TextView addressListTag;
        @BindView(R.id.address_list_edit)
        ImageView addressListEdit;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
